<template>
	<view class="pics-wrap">
		<scroll-view class="left" scroll-y="true">
			<view 
				:class="{active: index===active}" 
				v-for="(item, index) in categoryList" 
				:key="item.id"
				@click="leftClickHanlder(index,item.id)"
			>
				{{item.title}}
			</view>
		</scroll-view>
		<scroll-view scroll-y="true" class="right">
			<view v-for="(item, index) in categoryDetail" :key="item.id">
				<image :src="item.url" mode="" @click="previewImg(item.url)"></image>
				<text>{{item.title}}</text>
			</view>
			<view class="no-data" v-if="categoryDetail.length === 0">
				<text>暂无数据</text>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	import { reqImgCategory, reqImgCategoryDetail } from '@/api'
	export default {
		data() {
			return {
				categoryList: [],
				active: 0,
				categoryDetail: []
			};
		},
		onLoad() {
			this.getCategory()
		},
		methods:{
			// 获取商品分类
			async getCategory() {
				const result = await reqImgCategory()
				if(result.code === 200) {
					this.categoryList = result.data
					this.leftClickHanlder(0,this.categoryList[0].id)
				} else {
					uni.showToast({
						title:'获取失败！'
					})
				}
			},
			// 激活左侧导航
			async leftClickHanlder(index,pid) {
				this.active = index
				const result = await reqImgCategoryDetail(pid)
				if(result.code === 200) {
					this.categoryDetail = result.data
				} else {
					uni.showToast({
						title:'获取失败！'
					})
				}
			},
			// 预览图片
			previewImg(current) {
				const urls = this.categoryDetail.map(item => {
					return item.url
				})
				uni.previewImage({
					current,
					urls
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		height: 100%;
	}
	.pics-wrap {
		height: 100%;
		display: flex;
		.left {
				width: 200rpx;
				height: 100%;
				border-right: 1px solid #eee;
				view {
					height: 130rpx;
					line-height: 130rpx;
					color: #333;
					text-align: center;
					font-size: 30rpx;
					border-top: 1px solid #eee;
				}
				.active {
					color: #fff;
					background-color: $shop-color;
				}
		}
		.right {
			width: 550rpx;
			height: 100%;
			view {
				padding: 10rpx;
				margin-bottom: 20rpx;
			}
			.no-data {
				display: flex;
				align-items: center;
				justify-content: center;
			}
		}
	}
</style>
